/*
 * These files are distributed under the terms of the GNU GPLv2, the GNU General
 * Public version 2. See LICENSE.txt
 */
package fileCommodore.plugin.directoryCreator;

import java.awt.event.*;

import org.apache.commons.vfs.*;

import fileCommodore.model.*;
import fileCommodore.plugin.*;

/**
 * @author nado18@gmail.com, on Aug 1, 2007
 */
public class DirectoryCreatorPlugin extends Plugin
{
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see fileCommodore.plugin.Plugin#activeSelectionChanged()
	 */
	@Override
	public void activeSelectionChanged()
	{
		// TODO Auto-generated method stub
		
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see fileCommodore.plugin.Plugin#handle(java.awt.event.KeyEvent)
	 */
	@Override
	public void handle( KeyEvent ev )
	{
		if (KeyEvent.VK_F7 == ev.getKeyCode())
		{
			Selection selection = getActiveSelection();
			if (null != selection)
			{
				FileRowItem fri = selection.getCurrent();
				if (null != fri)
				{
					FileObject parent = fri.getFileObject();
					if (null != parent)
					{
						String folderName =
								ask(
									"New Folder Name",
									"Please enter the name of the new folder to create." );
						if (null != folderName)
						{
							try
							{
								FileObject child =
										parent.resolveFile( folderName );
								
								if (child.exists())
								{
									showWarning(
										"Directory Creation",
										"An item with the specified name already exists." );
								}
								else
								{
									child.createFolder();
								}
							}
							catch (Exception e)
							{
								showError(
									"Directory Creation Error",
									"There was an error creating the specified directory.",
									e );
							}
						}
					}
				}
			}
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see fileCommodore.plugin.Plugin#inactiveSelectionChanged()
	 */
	@Override
	public void inactiveSelectionChanged()
	{
		// TODO Auto-generated method stub
		
	}
	
}
